/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.repository;

import io.swagger.models.auth.In;
import me.zhengjie.modules.biz.domain.Sensor;
import me.zhengjie.modules.biz.domain.SensorLog;
import me.zhengjie.modules.system.domain.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
public interface SensorRepository extends JpaRepository<Sensor, Integer>, JpaSpecificationExecutor<Sensor> {
    @Query(value = "UPDATE hj_sensor SET data = ?1 WHERE slave_station_id = ?2 AND data_type = ?3",nativeQuery = true)
    void setCurrentData(float currentData,int slaveId,int type);

    @Query(value = "SELECT s.id FROM hj_sensor s WHERE s.slave_station_id = ?1",nativeQuery = true)
    int findBySensorId(int slaveId);

    @Query(value = "SELECT s.id FROM hj_sensor s WHERE s.type = ?1",nativeQuery = true)
    int findByType(int slaveId);

    @Query(value = "SELECT s.threshold_value FROM hj_sensor s WHERE slave_station_id = ?1",nativeQuery = true)
    float findByThresholdValue(int slaveId);

    @Query(value = "SELECT s.id FROM hj_sensor s WHERE slave_station_id = ?1 AND data_type = ?2",nativeQuery = true)
    int findByThresholdValueAndData_type(int slaveId,int type);

    @Query(value = "SELECT * FROM hj_sensor where id = ?1",nativeQuery = true)
    Sensor getSensor(int id);

    @Query(value = "SELECT id FROM hj_sensor WHERE type IN (1,2,3)",nativeQuery = true)
    ArrayList<Integer> findAllByIdAndType();

    Sensor findByLabIdAndType(Integer id, Integer type);

    @Query(value = "SELECT distinct id from hj_sensor where type in(1,2,3)",nativeQuery = true)
    List<Integer> findAllLogId();

    @Query(value = "SELECT id from hj_sensor where lab_id = ?1 and type IN (1,2,3)",nativeQuery = true)
    List<Integer> findAllByLabId(int labId);

    @Query(value = "SELECT id from hj_sensor where type = ?1",nativeQuery = true)
    List<Integer> findAllByType(int type);


    @Query(value = "SELECT id from hj_sensor where lab_id = ?1 and type = ?2",nativeQuery = true)
    List<Integer> findAllBySensorIdByLabIdType(Integer labId,Integer type);

    @Query(value = "SELECT s.* FROM hj_sensor s left join hj_lab l on (s.lab_id = l.id ) where l.floor = ?1",nativeQuery = true)
    List<Sensor> findByFloor(Integer floor);

    @Query(value = "SELECT l.floor, count(*) count FROM hj_sensor s, hj_lab l where l.id = s.lab_id and l.floor <> 1 group by floor order by floor",nativeQuery = true)
    List<Map<String, Object>> getCountByFloor();

    @Modifying
    @Transactional
    @Query(value = "delete from hj_sensor_log where created_at < ?2 and created_at > ?1",nativeQuery = true)
    void deleteAll(String s, String s1);
}
